Re: udp packet storms

Tim Newsham (newsham@zang.kcc.hawaii.edu)
Sun, 30 Oct 1994 16:00:06 -1000 (HST)

> 
> 
> To my knowledge, the broadcast trick will not work -- the "broadcast"
> will not go out on the ethernet or other broadcast address because the
> broadcast socket option will not have been selected and the packet
> will not be broadcast in reply. The echo loop between two hosts might,
> of course...
> 
> Perry

Run the following program.  Try "[name] 255.255.255.255" and
"[name] X.X.X.255" (assuming 8 bit subnetting).  Notice both
will work properly although I did not do a setsockopt with
SO_BROADCAST.

                                Tim N.


/* echo.c -  [name] [ip address] */
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/socket.h>
#include <netinet/in.h>

int hosts = 0;

im_done()
{
  printf("Done.  %d hosts responded\n", hosts);
  exit(0);
}

error(str)
char *str;
{
  perror(str);
  exit(1);
}

main(argc, argv)
char **argv;
{
  int s, adlen, val;
  char *message = "Echo Test", *remote;
  char buf[128];
  struct sockaddr_in ad;

  if(argc > 1)
    remote = argv[1];
  else
    remote = "127.0.0.1";

  signal(SIGINT, im_done);
  s = socket(AF_INET, SOCK_DGRAM, 0);
  if(s < 0) 
    error("socket");

  ad.sin_family = AF_INET;
  ad.sin_addr.s_addr = inet_addr(remote);
  ad.sin_port = htons(7);
  
  if(sendto(s, message, strlen(message), 0, &ad, sizeof(ad)) < 0)
    error("sendto");

  printf("Echo Test.  SIGINT to quit.\n");
  adlen = sizeof(ad);
  while(recvfrom(s, buf, 128, 0, &ad, &adlen) > 0) {
    printf("Reply from %s: %s\n", inet_ntoa(ad.sin_addr), buf);
    hosts++;
  }
}